package com.xiaoyun.constumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class RabbitReceive {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-1", declare = "true"),
            exchange = @Exchange(name = "exchange-1", durable = "true", type = "topic",
                    ignoreDeclarationExceptions = "true"),
            key = "spring.*"
    ))
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {
        log.info("===========================================");
        log.info("消费消息:{}", message.getPayload());
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}
